package com.example.activity;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Locale;

import com.example.test.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.app.Dialog;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

public class MapView extends FragmentActivity implements OnMapClickListener,
		OnMyLocationChangeListener {
	GoogleMap map;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapview);
		map = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map)).getMap();

		map.setOnMapClickListener(this);
		int status = GooglePlayServicesUtil
				.isGooglePlayServicesAvailable(getBaseContext());

		if (status != ConnectionResult.SUCCESS) { // Google Play Services are
			// not available
			int requestCode = 10;
			Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
					requestCode);
			dialog.show();

		} else { // Google Play Services are available

			// Getting reference to the SupportMapFragment of activity_main.xml
			SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
					.findFragmentById(R.id.map);

			// Getting GoogleMap object from the fragment
			map = fm.getMap();

			// Enabling MyLocation Layer of Google Map
			map.setMyLocationEnabled(true);

			// Setting event handler for location change
			map.setOnMyLocationChangeListener(this);

		}

	}

	@Override
	public void onMyLocationChange(Location location) {
		double latitude = location.getLatitude();

		// Getting longitude of the current location
		double longitude = location.getLongitude();

		// Creating a LatLng object for the current location
		LatLng latLng = new LatLng(latitude, longitude);

		// Showing the current location in Google Map
		map.moveCamera(CameraUpdateFactory.newLatLng(latLng));

		// Zoom in the Google Map
		map.animateCamera(CameraUpdateFactory.zoomTo(17));
	}

	@Override
	public void onMapClick(LatLng point) {
		map.clear();
		Geocoder gcoder = new Geocoder(this, Locale.KOREA);
		List<Address> addressed = null;
		double lat = point.latitude;
		double lon = point.longitude;
		DecimalFormat df = new DecimalFormat("#.0");
		String strLat=df.format(lat);
		String strLon=df.format(lon);
		String currentLocation=strLat+","+strLon;
		String title = "Latitude：" +currentLocation;
		Toast.makeText(this, "You are here:" + title, Toast.LENGTH_LONG).show();
		map.addMarker(new MarkerOptions().position(point).title(title));
		
		Intent add=new Intent(MapView.this,MainActivity.class);
		Bundle bundle=new Bundle();
		bundle.putString("CurrentLocation", currentLocation);
		add.putExtras(bundle);
		startActivity(add);
		
		

	}
	public boolean onMarkderClick(Marker arg0) {
		return false;

	}


}
